package top.humbleyuan.test;

import top.humbleyuan.leet.Array;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author HumbleYuan
 * @Date 2020/4/15 18:50
 * @Des
 */
public class HuaweiExamDemo {
    public static void main(String[] args) {

        //e1();

        //e2();

        //e3();

        int funNum;     // 函数个数
        int[][] callRelation;  //每个函数调用关系
        boolean isNA = false;   // 非法标志
        int[] stack;    // 每个函数的栈空间


        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()) {
            String[] firstLine = sc.nextLine().split(" ");  // 第一行数据
            funNum = Integer.valueOf(firstLine[0]);
            callRelation = new int[funNum][funNum];     // 表示每个函数不会重复调用其他函数?
            stack = new int[funNum];

            for (int i = 0; i < funNum; i++) {
                String[] funcLine = sc.nextLine().split(" ");

                // 输入非法
                if(funcLine.length == 1) {
                    isNA = true;
                    break;
                }

                stack[i] = Integer.valueOf(funcLine[1]);    // 栈空间内存输入
                String[] funCall = Arrays.copyOfRange(funcLine, 2, funcLine.length);
            }
        }

        sc.close();
    }

    private static void e3() {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()) {
            String[] fLine = sc.nextLine().split(" ");
            List<Integer[]> list = new ArrayList<>();

            for (int i = 0; i < Integer.valueOf(fLine[0]); i++) {
                String[] s = sc.nextLine().split(" ");
                Integer[] m = new Integer[s.length - 1];
                for (int j = 1; j < s.length; j++) {
                    m[j - 1] = Integer.valueOf(s[j]);
                }

                list.add(m);
            }

            e3_dfs(list.get(0),0,list, 0);

            System.out.println("NA");


        }

        sc.close();
    }

    public static Integer e3_dfs(Integer[] cur,int res,List<Integer[]> list,int max) {
        if(cur.length == 1) {
            max = res > max ? res : max;
        }

        for (int i = 1; i < cur.length; i++) {
            res += cur[0];

            e3_dfs(list.get(cur[i]), res, list, max);

            // 回溯
            res -= cur[0];
        }

        return max;

    }

    private static void e2() {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()) {

            String[] strs = sc.nextLine().split(" ");
            String keyword = strs[0];

            String[] single = strs[1].split("],");


            single[single.length - 1] = single[0].replaceAll("]","");
            List<String> list = new ArrayList<>();

            for (int i = 0; i < single.length; i++) {

                String[] sp = single[i].split("\\[");

                if(!sp[0].equals(keyword)) {
                    break;
                }

                String[] info = sp[1].split(",");
                String res = "";
                for (int j = 0; j < info.length; j++) {
                    String[] singleInfo = info[j].split("=");
                    if(j == 0 && singleInfo[0].equals("addr")) {
                        res += extractStr(singleInfo[1]);
                    }

                    if(j == 1 && singleInfo[0].equals("mask")) {
                        res += extractStr(singleInfo[1]);
                    }

                    if(j == 2 && singleInfo[0].equals("val")) {
                        res += extractStr(singleInfo[1]);
                    }
                }

                String[] r = res.split(" ");

                if(r.length == 3) {
                    list.add(res);
                }

            }

            if(list.size() == 0) {
                System.out.println("FAIL");
            } else {
                for (String s:list) {
                    System.out.println(s);
                }
            }
        }

        sc.close();
    }

    // e2代码抽取，addr=0x34的判断提取
    public static String extractStr(String s) {
        String res = "";

        // 防止错误如0x的出现
        if(s.length() <= 2) {
            return res;
        }
        String form = String.valueOf(s.charAt(0)) + String.valueOf(s.charAt(1));
        if(form.equals("0x") || form.equals("0X")) {
            res += s + " ";
        }

        return res;
    }

    private static void e2_2() {

        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        Pattern pattern = Pattern.compile(str +
                "\\[addr=(0[xX][0-9a-fA-F]+),mask=(0[xX][0-9a-fA-F]+),val=(0[xX][0-9a-fA-F]+)\\]?");
        String[] strings = sc.next().split("],");
        for(String s : strings){
            Matcher matcher = pattern.matcher(s);
            if (matcher.matches()) {
                System.out.println(matcher.group(1) + " "
                        + matcher.group(2) + " " +matcher.group(3));
            }
        }
        sc.close();
    }


    private static void e1() {


        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()) {
            String[] votes = sc.nextLine().trim().split(",");

            if(votes == null || votes.length == 0) {
                System.out.println("error.0001");
                return;
            }

            HashMap<String, Integer> map = new HashMap<>();
            for (int i = 0; i < votes.length; i++) {
                // 处理非法输入
                for (int j = 0; j < votes[i].length(); j++) {
                    if(j == 0 && !(votes[i].charAt(j) >= 'A' && votes[i].charAt(j) <= 'Z')) {
                        System.out.println("error.0001");
                        return;
                    }

                    if(j > 0 && !(votes[i].charAt(j) >= 'a' && votes[i].charAt(j) <= 'z')) {
                        System.out.println("error.0001");
                        return;
                    }
                }


//
//                if(!map.containsKey(votes[i])) {
//                    map.put(votes[i], 1);
//                    continue;
//                }
//
//                map.put(votes[i], map.get(votes[i]) + 1);

                // 写法简化
                map.put(votes[i], map.getOrDefault(votes[i], 0) + 1);
            }



            PriorityQueue<String> top = new PriorityQueue<>(1);
            int max = 0;
            for (String s : map.keySet()) {
                if(map.get(s) >= max) {
                    max = map.get(s);
                    top.offer(s);
                }
            }

            System.out.println(top.peek());
        }

        sc.close();
    }
}
